package com.utils.conmponentbase.tools;


import android.content.Context;

import com.azhon.appupdate.config.UpdateConfiguration;
import com.azhon.appupdate.manager.DownloadManager;
import com.blankj.utilcode.util.AppUtils;
import com.utils.conmponentbase.R;


/**
 * BaseAppPackage com.appprogram.baseapppackage.util
 * 2020-01-08 16:05
 * APP升级帮助类，自动安装 @See https://github.com/azhon/AppUpdate
 */
public class AppUpdateHelper {
    private static AppUpdateHelper sAppUpdateUtil;

    public static AppUpdateHelper getInstance() {
        if (null == sAppUpdateUtil) {
            synchronized (AppUpdateHelper.class) {
                if (null == sAppUpdateUtil) sAppUpdateUtil = new AppUpdateHelper();
            }
        }
        return sAppUpdateUtil;
    }

    public void update(Context pContext, String apkUrl, String title, String updateContent) {
        //使用方法 https://github.com/azhon/AppUpdate
        UpdateConfiguration configuration = new UpdateConfiguration().setForcedUpgrade(true);
        DownloadManager manager = DownloadManager.getInstance(pContext);
        configuration.setEnableLog(true)
                .setJumpInstallPage(true)
                .setJumpInstallPage(true)
                .setShowNotification(true)
                .setShowBgdToast(true)
                .setForcedUpgrade(true);
        manager.setApkName("App.apk")
                .setAuthorities(AppUtils.getAppPackageName())
                .setApkUrl(apkUrl)
                .setShowNewerToast(true)
                .setConfiguration(configuration)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setApkVersionCode(AppUtils.getAppVersionCode() + 1)
                .setApkVersionName(AppUtils.getAppVersionName())
                .setApkDescription(updateContent)
                .download();
    }
}
